<?php

require_once(dirname(__FILE__) . '/appconf.php');

class Test extends Site {

    function setup() {
        set_time_limit(0);
        $this->run_modes('test');
        $this->start_mode('test');
        $this->selfurl = '/testxml_report.php';
        parent::setup();
    }

    function cgiapp_prerun() {

        parent::cgiapp_prerun();
    }

    function action_test() {

        $page = intval($this->get_q('page', 1, 'int'));
        $pagesize = 10;
        $root = $this->param('WWW');

        $param = array();
        $param['field'] = "";
        $param['where'] = "";
        //$param['where'] = "num=5850";
        $param['order'] = "id asc"; //"id asc"
        $param['group'] = "";
        $param['limit'] = "";
        $param['pagesize'] = $pagesize;
        $param['page'] = $page;
        $param['join'] = "";
        $param['on'] = "";
        $data = db_query_hash('fg_product', $param);

        if ($data) {

            if ($page > $data['pageinfo']['pagecount']) {

                echo '完成全部轉換';
                exit();
            }

            if ($data['result']) {
                foreach ($data['result'] as $value) {
                    // if($value['is_report']==1)                        continue;
                    echo $url = "http://www.fashionguide.com.tw/beauty/Api/msg_imore.asp?act=20090821&diaxczh=ieqoj&Num=" . $value['num'];

                    $xmlObj = simplexml_load_file($url);

                    if ($xmlObj) {
                        $is_report = 0;
                        $vote_option_array = array();

                        foreach ($xmlObj as $key_row => $value_row) {
                            if ($key_row == 'topictheme') {

                                $vote_option_type = array();
                                $vote_option_type_data = db_query_hash('fg_vote_option_type', array('field' => 'id,cname', 'where' => 'status=1 and cname="' . $this->xml2Str($value_row->topicintro) . '"'));

                                if ($vote_option_type_data['result'])
                                    $vote_option_type = $vote_option_type_data['result'][0];
                                if ($vote_option_type) {
                                    $vote_option_type_id = $vote_option_type['id'];
                                } else {
                                    $insert = array();
                                    $insert['cname'] = $this->xml2Str($value_row->topicintro);
                                    $vote_option_type_id = db_add('fg_vote_option_type', $insert);
                                }


                                $vote_option_array[] = array('topicintro' => $vote_option_type_id, 'topicstar' => $this->xml2Str($value_row->topicstar));
                            } else {

                                if ($value_row) {
                                    $msgtheme_array = array();
                                    foreach ($value_row as $key_msgs => $value_msgs) {
                                        if ($key_msgs == 'msgtheme') {
                                            $vote_option_type = array();
                                            $vote_option_type_data = db_query_hash('fg_vote_option_type', array('field' => 'id,cname', 'where' => 'status=1 and cname="' . $this->xml2Str($value_msgs->msgintro) . '"'));
                                            if ($vote_option_type_data['result'])
                                                $vote_option_type = $vote_option_type_data['result'][0];

                                            if ($vote_option_type) {
                                                $vote_option_type_id = $vote_option_type['id'];
                                            } else {
                                                $insert = array();
                                                $insert['cname'] = $this->xml2Str($value_msgs->msgintro);
                                                $vote_option_type_id = db_add('fg_vote_option_type', $insert);
                                            }

                                            $msgtheme_array[] = array('msgintro' => $vote_option_type_id, 'msgstar' => $this->xml2Str($value_msgs->msgstar));
                                        }
                                    }

                                    $insert_array = array("msgname", "msgskin", "msgage", "msgitem");

                                    $insert = array();
                                    foreach ($insert_array as $insert_value) {

                                        $insert[$insert_value] = $this->xml2Str($value_row->$insert_value);
                                    }

                                    $msg = $this->xml2Str($value_row->msg);
                                    $pattern_caption = '/<a onclick(.*?)id="(.*?)"(.*?)a>/i';
                                    $replacement_caption = '<img src="${2}" />';
                                    $content = preg_replace($pattern_caption, $replacement_caption, $msg);
                                    $img_array = array();

                                    $reg = "/<img[^>]*src=\"(http:\/\/(.+)\/(.+)\.(jpg|gif|bmp|png))\"/isU";
                                    preg_match_all($reg, $content, $img_array, PREG_PATTERN_ORDER);
                                    $img_array = array_unique($img_array[1]);

                                    if ($img_array) {
                                        foreach ($img_array as $value_imgName) {
                                            $ret = uploadfiles($root . '/upload/fg_report', $value_imgName);

                                            if ($ret[0]['code'] == 1) {
                                                $imgstr = str_replace($root, '', $ret[0]['path']);
                                                $content = str_replace($value_imgName, $imgstr, $content);
                                            }
                                        }
                                    }
                                    $insert['pic_num'] = $img_array ? count($img_array) : 0;
                                    $insert['msg'] = $content;

                                    $ampm = "";
                                    $time_array = explode(" ", trim($this->xml2Str($value_row->msgdate)));
                                    if ($time_array[1] == "上午") {
                                        $ampm = "AM";
                                    } elseif ($time_array[1] == "下午") {
                                        $ampm = "PM";
                                    }

                                    $insert['msgdate'] = date('Y-m-d H:i:s', strtotime($time_array[0] . " " . $time_array[2] . " " . $ampm));
                                    $insert['num'] = $value['num'];

                                    $ret = uploadfiles($root . '/upload/fg_avatar', trim($this->xml2Str($value_row->msgavatar)));

                                    if ($ret[0]['code'] == 1) {
                                        $imgavatar = str_replace($root, '', $ret[0]['path']);
                                        $insert['msgavatar'] = $imgavatar;
                                    }


                                    $insert['vote_option_json'] = json_encode($msgtheme_array);


                                    db_add('fg_report', $insert);
                                    $is_report = 1;
                                    echo " 成功<br>";
                                }
                            }
                        }

                        if ($is_report) {
                            db_update('fg_product', array('vote_option_json' => json_encode($vote_option_array), 'is_report' => 1), 'num=' . $value['num']);
                        }
                        echo $value['num'] . "<br>";
                    }
                }
            } else {
                echo "無數據";
            }
            echo '<meta http-equiv="refresh" content="1;url= http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?action=test&page=' . ($page + 1) . '">';

            echo '開始ID ' . $data['result']['0']['id'] . ' 完成本頁，繼續下頁';
        } else {
            echo "無數據";
        }
    }

    function xml2Str($obj) {
        return trim((string) $obj);
    }

}

header("Content-type: text/html; charset=utf-8");
$webapp = new Test();
$webapp->run();
?>